package com.hqyj.springBoot.modules.account.dao;

import com.hqyj.springBoot.modules.account.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface RoleDao {

    @Insert("insert into account_role (create_date,role_name) values (#{createDate},#{roleName})")
    @Options(useGeneratedKeys = true, keyColumn = "id" ,keyProperty = "roleId")
    void insertRole(Role role);

    @Select("select * from account_role where role_name = #{roleName}")
    Role selectRoleByRoleName(String roleName);

    @Update("update account_role set create_date = #{createDate} , role_name = #{roleName}" +
            "where id = #{roleId}")
    void updateRole(Role role);

    @Delete("delete from account_role where id = #{roleId}")
    void deleteRoleById(int roleId);

    @Select("select * from account_role where id = #{roleId}")
    @Result(column = "id",property = "roleId")
    Role selectRoleById(int roleId);

    @Select("SELECT r.* FROM `account_user_role` ur ,account_role r, account_user u " +
            "where u.id = ur.user_id and r.id = ur.role_id and u.id = #{userId}")
    @Result(column = "id",property = "roleId")
    List<Role> getRolesByUserId(int userId);


    @Select("select r.* from account_role r,account_resource re,account_role_resource arr " +
            "where r.id = arr.role_id and re.id = arr.resource_id and re.id = #{resourceId};")
    @Result(column = "id",property = "roleId")
    List<Role> getRolesByResourceId(int resourceId);

    @Select("select * from account_role")
    @Result(column = "id",property = "roleId")
    List<Role> getAllRoles();


}
